package com.qdxht.dispenser.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.qdxht.dispenser.R;
import com.qdxht.dispenser.ui.models.entity.UserInfoEntity;
import com.hjq.toast.ToastUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


public class ViewUtil {

	/**
	 * 关闭软键盘
	 * 
	 * @param activity
	 *            layout布局ID
	 */
	public static void colseSoftInput(final Activity activity, View view) {
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				colseSoftInput(activity);
			}
		});
	}

	public static void colseSoftInput(final Activity activity, int id) {
		View view = (View) activity.findViewById(id);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				colseSoftInput(activity);
			}
		});
	}

	/**
	 * 关闭软键盘
	 * @param activity
	 */
	public static void colseSoftInput(final Activity activity) {
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		if(activity.getCurrentFocus() != null){
//			imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
		}
	}

	public static void colseSoftInput(final Context activity, EditText mEditText) {
		InputMethodManager imm = (InputMethodManager) activity
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(mEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}



	/**
	 * 获取屏幕宽度
	 * @param context	
	 * @return 适配后的高度
	 */
	public static int getScreenWidth(Context context) {
		// TODO Auto-generated method stub
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics metrics = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(metrics);
		return metrics.widthPixels;
	}

	/**
	 * 设置文本框右侧删除图标的删除全部文本功能，实现动态显示和隐藏删除按钮
	 * @Title: setEditTextDeleteListener 
	 * @Description: 
	 * @param activity	所在activity
	 * @param closeResId	删除按钮
	 */
	public static void setEditTextDeleteListener(final Activity activity, int editResId, int closeResId) {
		final EditText editView = (EditText) activity.findViewById(editResId);
		final View closeView = activity.findViewById(closeResId);
		closeView.setVisibility(View.GONE);
		closeView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				editView.setText("");
			}
		});
		
		editView.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				if(s.length() == 0){
					closeView.setVisibility(View.GONE);
				}else{
					closeView.setVisibility(View.VISIBLE);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	/**
	 * 设置密码框文本的可见性
	 * @Title: setShowPwdTextListener 
	 * @Description: 
	 * @param activity
	 * @param pwdEditResId 密码框
	 * @param visibleResId 点击按钮
	 */
	public static void setShowPwdTextListener(final Activity activity, int pwdEditResId, int visibleResId) {
		final EditText pwdEditView = (EditText) activity.findViewById(pwdEditResId);
		final View visibleView = activity.findViewById(visibleResId);
		visibleView.setVisibility(View.GONE);
		visibleView.setOnClickListener(new OnClickListener() {
			Boolean isShowPwd = false;
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				visibleView.geti
				isShowPwd = !isShowPwd;
				if(isShowPwd){
					//如果选中，显示密码
					pwdEditView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
					//v.setBackgroundResource(R.drawable.icon_login_visible_press);
				}else{
					//否则隐藏密码
					pwdEditView.setTransformationMethod(PasswordTransformationMethod.getInstance());
					//v.setBackgroundResource(R.drawable.icon_login_visible_normal);
				}
				pwdEditView.setSelection(pwdEditView.getText().toString().length());
			}
		});
		
		pwdEditView.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				if(s.length() == 0){
					visibleView.setVisibility(View.GONE);
				}else{
					visibleView.setVisibility(View.VISIBLE);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

	public static void setText(Activity context,int id,String text){
		TextView textView = context.findViewById(id);
		if(text != null && !"".equals(text) && !"null".equals(text)){
			textView.setText(text);
		} else {
			textView.setText("");
		}
	}

	public static void setText(Activity context,int id,Object text){
		TextView textView = context.findViewById(id);
		if(text != null && !"".equals(text.toString()) && !"null".equals(text.toString())){
			textView.setText(text.toString());
		} else {
			textView.setText("");
		}
	}

	public static void setText(View context,int id,String text){
		TextView textView = context.findViewById(id);
		if(text != null && !"".equals(text) && !"null".equals(text)){
			textView.setText(text);
		} else {
			textView.setText("");
		}
	}

	public static void setText(View context,int id,Object text){
		TextView textView = context.findViewById(id);
		if(text != null && !"".equals(text) && !"null".equals(text)){
			textView.setText(text.toString());
		} else {
			textView.setText("");
		}
	}

	public static void setText(TextView textView,String text){
		if(text != null && !"".equals(text) && !"null".equals(text)){
			textView.setText(text);
		} else {
			textView.setText("");
		}
	}

	public static void setText(TextView textView,Object text){
		if(text != null && !"".equals(text.toString()) && !"null".equals(text.toString())){
			textView.setText(text.toString());
		} else {
			textView.setText("");
		}
	}

	public static void setTextAndParentViewGone(View context,int id,String text){
		TextView textView = context.findViewById(id);
		if(text != null && !"".equals(text) && !"null".equals(text)){
			textView.setText(text);
		} else {
			setParentViewGone(textView);
		}
	}

	public static void setViewGone(View context,int id){
		if(context == null) {
			return;
		}
		TextView view = context.findViewById(id);
		setViewGone(view);
	}


	public static void setViewVisible(View context,int id){
		if(context == null) {
			return;
		}
		TextView view = context.findViewById(id);
		setViewVisible(view);
	}

	public static void setViewInvisible(View context,int id){
		if(context == null) {
			return;
		}
		TextView view = context.findViewById(id);
		setViewInvisible(view);
	}

	public static void setViewGone(View view){
		if(view != null) {
			view.setVisibility(View.GONE);
		}
	}

	public static void setViewVisible(View view){
		if(view != null) {
			view.setVisibility(View.VISIBLE);
		}
	}

	public static void setViewInvisible(View view){
		if(view != null) {
			view.setVisibility(View.INVISIBLE);
		}
	}

	public static void setParentViewGone(View view){
		if(view != null) {
			((View)view.getParent()).setVisibility(View.GONE);
		}
	}

	public static void setTextColor(TextView view, int color){
		if(view != null){
			view.setTextColor(color);
		}
	}

	public static void setTextColor(View context,int id, int color){
		TextView textView = context.findViewById(id);
		if(textView != null){
			textView.setTextColor(color);
		}
	}

	public static void setImageResource(View context,int id, int resId){
		ImageView view = context.findViewById(id);
		if(view != null){
			view.setImageResource(resId);
		}
	}

	public static Bitmap loadBitmapFromViewBySystem(View v) {
		if (v == null) {
			return null;
		}
		v.setDrawingCacheEnabled(true);
		v.buildDrawingCache();
		Bitmap bitmap = v.getDrawingCache();
		return bitmap;
	}


	/**
	 * @param bmp 获取的bitmap数据
	 * @param picName 自定义的图片名
	 */
	public static void saveBmp2Gallery(Context mContext, Bitmap bmp, String picName) {
		String fileName = null;
		//系统相册目录
		String galleryPath= Environment.getExternalStorageDirectory()+"/倾语/image";
		// 声明文件对象
		File fileImagePath = null;
		// 声明输出流
		FileOutputStream outStream = null;
		try {
			File mImageStoreDir = new File(galleryPath);
			if (!mImageStoreDir.exists()) {
				mImageStoreDir.mkdirs();
			}
			// 如果有目标文件，直接获得文件对象，否则创建一个以filename为名称的文件
			fileImagePath = new File(galleryPath, picName+ ".jpg");
			// 获得文件相对路径
			fileName = fileImagePath.toString();
			// 获得输出流，如果文件中有内容，追加内容
			outStream = new FileOutputStream(fileName);
			if (null != outStream) {
				bmp.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
			}
		} catch (Exception e) {
			e.getStackTrace();
		}finally {
			try {
				if (outStream != null) {
					outStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//通知相册更新
		MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
				bmp, fileName, null);
		Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
		Uri uri = Uri.fromFile(fileImagePath);
		intent.setData(uri);
		mContext.sendBroadcast(intent);
		ToastUtils.show("图片保存成功");
	}

}
